package app.mvc.model.keyboards
{
	import alternativa.gui.keyboard.IKeyFilter;
	import alternativa.gui.keyboard.KeyboardManager;
	
	import app.consts.CommandType;
	import app.consts.XDirection;
	import app.mvc.AppFacade;
	import app.mvc.Notifications;
	import app.mvc.controller.editor.MoveObjectCommand;
	import app.mvc.view.components.dragDrop.DropLayer;
	
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import globals.Config;
	
	import org.puremvc.as3.patterns.facade.Facade;
	
	import utils.UtilsEditorCommand;
	
	public class ObjectLayerKeyFilter implements IKeyFilter
	{
		private var _keyCode:int=0;
		
		//各个逻辑
		private var continueMoveX:int=0;
		private var continueMoveY:int=0;
		
		public function ObjectLayerKeyFilter()
		{
		}

		/**
		 * 键盘按下事件
		 */
		public function onKeyDown(...rest):void
		{
			switch(_keyCode)
			{
				case Keyboard.UP:
					moveStep(XDirection.UP,KeyboardManager.shiftKey?Config.moveStep*10:Config.moveStep);
					break;
				case Keyboard.DOWN:
					moveStep(XDirection.DOWN,KeyboardManager.shiftKey?Config.moveStep*10:Config.moveStep);
					break;
				case Keyboard.LEFT:
					moveStep(XDirection.LEFT,KeyboardManager.shiftKey?Config.moveStep*10:Config.moveStep);
					break;
				case Keyboard.RIGHT:
					moveStep(XDirection.RIGHT,KeyboardManager.shiftKey?Config.moveStep*10:Config.moveStep);
					break;
			}
		}
		
		public function onKeyUp(...rest):void
		{
			switch(_keyCode)
			{
				case Keyboard.DELETE:
					var deleteArray:Array = DropLayer.getSelectListArray();
					AppFacade.getInstance().sendNotification(Notifications.CMD_TO_REMOVE_OBJECT,deleteArray);
					break;
				case Keyboard.UP:
				case Keyboard.DOWN:
				case Keyboard.LEFT:
				case Keyboard.RIGHT:
					var paramObj:Object = {};
					paramObj.selectedObjects = DropLayer.getSelectListArray();
					paramObj.changeX = continueMoveX;
					paramObj.changeY = continueMoveY;
					UtilsEditorCommand.addToHistory(UtilsEditorCommand.createCommand(MoveObjectCommand,paramObj));
					continueMoveX=0;
					continueMoveY=0;
					break;
			}
		}
		
		public function filter(e:KeyboardEvent):Boolean
		{
			_keyCode = e.keyCode;
			switch(e.keyCode)
			{
				case Keyboard.UP:
				case Keyboard.DOWN:
				case Keyboard.LEFT:
				case Keyboard.RIGHT:
				case Keyboard.DELETE:
					return true;
					break;
			}	

			return false;
		}
		
		public function get keyCode():Array
		{
			return [_keyCode];
		}
		
		/**
		 * 移动步进
		 * @param $direction
		 * @param $step
		 */
		private function moveStep($direction:int=0,$step:int=1):void
		{
			var paramObj:Object = [];
			var changeX:int=0;
			var changeY:int=0;
		
			switch($direction)
			{
				case XDirection.LEFT:
					changeX=-$step;
					break;
				case XDirection.UP:
					changeY=-$step;
					break;
				case XDirection.RIGHT:
					changeX=$step;
					break;
				case XDirection.DOWN:
					changeY=$step;
					break;
			}
			
			paramObj.selectedObjects = DropLayer.getSelectListArray();
			paramObj.changeX = changeX;
			paramObj.changeY = changeY;

			AppFacade.getInstance().sendNotification(Notifications.CMD_TO_MOVE_OBJECTS,paramObj,CommandType.NOT_IN_HISTORY);
			continueMoveX+=changeX;
			continueMoveY+=changeY;
		}
	}
}